<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2015 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Newsclass.php 09594 2018-09-04 09:29:53 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_newsclass
 * +----------------------------------------------------------------------
 */

namespace application\admin\controller;

use ticky\request;
use ticky\response;
use ticky\tree;

class menu extends base {

    public function index() {
        $tree = new tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $data = db('menu')->order('listorder ASC')->select();
        $array = array();
        foreach ($data as $v) {
            $addurl = '/admin/menu/add/?action=add&parentid=' . $v['id'] . '&id=' . $v['id'];
            $editurl = '/admin/menu/update/?action=edit&id= ' . $v['id'] . '&parentid=' . $v['parentid'];
            $delurl = '/admin/menu/delete/?action=del&id= ' . $v['id'];

            $v['display'] = $v['display'] ? '<span style = "color:red">是</span>' : '否';
            if ($v['a'] == 'index') {
                $funurl = '/admin/menu/funall/?action=add&parentid=' . $v['id'] . '&id=' . $v['id'];
                $v['name'] = '<a title = "增加子类" href = "javascript:;" onclick = "layer_open(\'增加子类\',\'' . $funurl . '\',500,300)" class = "red">' . $v['name'] . '</a>';
            }
            $v['string'] = '<a title = "增加子类" href = "javascript:;" onclick = "layer_open(\'增加子类\',\'' . $addurl . '\',800,500)" class = "btn btn-primary btn-xs">增加子类</a>
            <a title = "编辑栏目" href = "javascript:;" onclick = "layer_open(\'编辑菜单\',\'' . $editurl . '\',800,500)" class = "btn btn-success btn-xs">编辑</a>
            <a title = "删除" onclick = "layer_del(\'' . $delurl . '\')" class = "btn btn-warning btn-xs">删除</a>';
            $array[] = $v;
        }
        $str = "<tr class='text-c'>
                        <td><input type='text' class='input-text listorder' name='listorder[\$id]' value='\$listorder' style='width: 50px;'></td>
                        <td>\$id</td>
                        <td class='text-l'><i class='fa \$icon fa-fw'></i>\$spacer\$name</td>
                        <td>\$m</td>
                        <td>\$c</td>
                        <td>\$a</td>
                        <td>\$display</td>
                        <td class='td-manage'>\$string</td>
                </tr>";

        $tree->init($array);
        $menus = $tree->get_tree(0, $str);
        //debug($menus);
        $this->assign('items', $menus);
        $this->display('menu/index');
    }

    // 添加记录
    public function add() {
        if (request::isPost()) {
            $data = $this->post_frm();
            db('menu')->insert($data);
            cache('menu_string_' . session('roleid'), null);
            showmsg('添加成功', '/admin/menu', 'y');
        } else {
            $item = db('menu')->where(['id' => request::get('id', 0)])->find();
            $this->assign('postUrl', '/admin/menu/add');
            $this->assign('action', '添加');
            $item['display'] = 1;
            $item['name'] = '';
            $this->assign('item', $item);
            $this->assign('select_menus', $this->select_menus());
            $this->display('menu/manage');
        }
    }

    // 更新记录
    public function update() {
        if (request::isPost()) {
            $data = $this->post_frm();
            $id = request::post('id', 0);
            db('menu')->where(['id' => $id])->update($data);
            cache('menu_string_' . session('roleid'), null);
            showmsg('添加成功', '/admin/menu', 'y');
        } else {
            $item = db('menu')->where(['id' => request::get('id', 0)])->find();
            $this->assign('postUrl', '/admin/menu/update');
            $this->assign('action', '修改');
            $this->assign('item', $item);
            $this->assign('select_menus', $this->select_menus());
            $this->display('menu/manage');
        }
    }

    // 删除记录
    public function delete() {
        $id = request::get('id', 0);
        db('menu')->delete(['id' => $id]);
        cache('menu_string_' . session('roleid'), null);
        showmsg('删除成功', '/admin/menu');
    }

    //菜单排序
    public function sort() {
        if (isset($_POST['dosubmit'])) {
            foreach ($_POST['listorder'] as $id => $listorder) {
                db('menu')->where(['id' => $id])->update(array('listorder' => $listorder));
            }
            cache('menu_string_', null);
            showmsg('排序成功', '/admin/menu');
        }
    }

    //添加常用子菜单
    public function funall() {
        $menu = db('menu')->where(['id' => request::get('id', 0)])->find();
        if (request::isPost()) {
            $name = str_replace('管理', '', $menu['name']);
            $name = str_replace('后台', '', $name);
            $action = request::post('action', []);
            for ($i = 0; $i < count($action); $i++) {
                $l = L($action[$i]);
                if (strpos($l, $name) !== false) {
                    $meun_name = $l;
                } else {
                    $meun_name = $l . $name;
                }
                db('menu')->insert(array('name' => $meun_name, 'parentid' => $menu['id'], 'm' => $menu['m'], 'c' => $menu['c'], 'a' => $action[$i], 'data' => '', 'listorder' => $i, 'display' => '0'));
            }
            showmsg('添加权限成功', '/admin/menu');
        } else {
            $all_action = $this->get_action_all($menu['m'], $menu['c']);
            $this->assign('file', 'application/' . $menu['m'] . '/controller/' . $menu['c'] . '.php');
            $this->assign('menu', $menu);
            $this->assign('all_action', $all_action);
            $this->display('menu/funall');
        }
    }

    //表单数据
    private function post_frm() {
        $arr = array();
        $arr['name'] = request::post('name', '');
        $arr['parentid'] = request::post('parentid', '');
        $arr['m'] = request::post('m', '');
        $arr['c'] = request::post('c', '');
        $arr['a'] = request::post('a', '');
        $arr['data'] = request::post('data', '');
        $arr['icon'] = request::post('icon', 'fa-heart-o');
        $arr['listorder'] = request::post('listorder', '0');
        $arr['display'] = request::post('display', '0');
        return $arr;
    }

    private function get_action_all($m, $c) {
        $file = APP_PATH . '/' . $m . '/controller/' . $c . '.php';  //控制器路径
        if (file_exists($file)) {
            $content = file_get_contents($file);
            preg_match_all("/.*?public.*?function(.*?)\(.*?\)/i", $content, $matches);
            $functions = $matches[1];
            //排除部分方法
            $inherents_functions = array('index', 'search_frm', 'post_frm', '_initialize', '__construct', 'getActionName', 'isAjax', 'display', 'show', 'fetch', 'buildHtml', 'assign', '__set', 'get', '__get', '__isset', '__call', 'error', 'success', 'ajaxReturn', 'redirect', '__destruct', '_empty');
            foreach ($functions as $func) {
                $func = trim($func);
                if (!in_array($func, $inherents_functions)) {
                    $all_action[] = $func;
                }
            }
            return array_filter($all_action);
        } else {
            return false;
        }
    }

    private function select_menus() {
        $parentid = isset($_GET['parentid']) ? intval($_GET['parentid']) : 0;
        $tree = new tree();
        $data = db('menu')->order('listorder ASC, id DESC')->select();
        $array = array();
        foreach ($data as $v) {
            $v['selected'] = $v['id'] == $parentid ? 'selected' : '';
            $array[] = $v;
        }
        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $select_menus = $tree->get_tree(0, $str);
        return $select_menus;
    }

}
